package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.borrowworker.BorrowHelper;
import com.qdlc.p2p.biz.borrowworker.worker.BorrowWorker;
import com.qdlc.p2p.biz.executer.AbstractExecuter;
import com.qdlc.p2p.biz.executer.ExecuterHelper;
import com.qdlc.p2p.biz.executer.impl.BorrowTenderExcuter;
import com.qdlc.p2p.biz.executer.impl.ExperienceExecuter;
import com.qdlc.p2p.biz.executer.impl.ExperienceTenderExecuter;
import com.qdlc.p2p.biz.executer.impl.InterestExchangeExecuter;
import com.qdlc.p2p.biz.executer.impl.UseRateInterestExecuter;
import com.qdlc.p2p.biz.service.BorrowTenderService;
import com.qdlc.p2p.biz.service.CollectPayService;
import com.qdlc.p2p.biz.service.TppTradeService;
import com.qdlc.p2p.biz.service.UserStatisticsService;
import com.qdlc.p2p.biz.util.IdcardUtils;
import com.qdlc.p2p.common.constant.BorrowTenderStatus;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.constant.SinaConstant;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.exception.RedEnvelopeException;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.OrderNoUtils;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.BorrowCollectionDao;
import com.qdlc.p2p.dal.dao.BorrowDao;
import com.qdlc.p2p.dal.dao.BorrowTenderDao;
import com.qdlc.p2p.dal.dao.UserBaseInfoDao;
import com.qdlc.p2p.dal.dto.Borrow;
import com.qdlc.p2p.dal.dto.BorrowTender;
import com.qdlc.p2p.dal.dto.TppTradeLog;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.BorrowModel;
import com.qdlc.p2p.dal.model.BorrowTenderModel;
import com.qdlc.p2p.dal.model.RankModel;
import com.qdlc.p2p.dal.model.TenderDynamicModel;
import com.qdlc.p2p.dal.model.interest.InterestCalculator;
import com.qdlc.p2p.dal.util.SystemConfigs;

@Service("borrowTenderService")
public class BorrowTenderServiceImpl implements BorrowTenderService {

    private final static Logger logger = Logger.getLogger(BorrowTenderServiceImpl.class);

    @Resource
    private BorrowTenderDao borrowTenderDao;

    @Resource
    private BorrowDao borrowDao;

    @Resource
    private UserBaseInfoDao userInfoDao;

    @Resource
    private UserStatisticsService userStatisticsService;

    @Resource
    private BorrowCollectionDao borrowCollectionDao;

    @Resource
    private TppTradeService tppTradeService;
    
    @Resource
    @Qualifier("experienceCollectService")
    private CollectPayService collectService;

    @Override
    public double investIngAmount(long userId) {
        return borrowTenderDao.investIngAmount(userId);
    }

    @Override
    public double investAmount(long userId) {
        return borrowTenderDao.investAmount(userId);
    }

    @Override
    public PageDataList<BorrowTenderModel> list(BorrowTenderModel model) {
        QueryParam param = QueryParam.getInstance();
        if (model != null) {
            if (StringUtil.isNotBlank(model.getSearchName())) {
                SearchFilter orFilter1 = new SearchFilter("user.userName", Operators.LIKE,
                        model.getSearchName().trim());
                SearchFilter orFilter2 = new SearchFilter("user.realName", Operators.LIKE,
                        model.getSearchName().trim());
                param.addOrAndFilter(orFilter1, orFilter2);
            } else {
                if ((model.getUser() != null) && (model.getUser().getUserId() > 0)) {
                    param.addParam("user.userId", model.getUser().getUserId());
                }
                if (StringUtil.isNotBlank(model.getUserName())) {
                    param.addParam("user.userName", Operators.LIKE, model.getUserName().trim());
                }
                if (model.getBorrowType() != 0) {
                    param.addParam("borrow.type", model.getBorrowType());
                }
                if (model.getCategory() != 99) {
                    if (model.getCategory() == 98)
                        param.addParam("borrow.category", Operators.NOTEQ, 2);
                    else
                        param.addParam("borrow.category", model.getCategory());
                }
                // 投标记录真实姓名搜索添加 XIANGMUYH-145 qpzx 2015-05-09 start
                if (StringUtil.isNotBlank(model.getRealName())) {
                    param.addParam("user.realName", Operators.LIKE, model.getRealName().trim());
                }
                // 投标记录真实姓名搜索添加 XIANGMUYH-145 qpzx 2015-05-09 end
                if (StringUtil.isNotBlank(model.getBorrowName())) {
                    param.addParam("borrow.name", Operators.LIKE, model.getBorrowName().trim());
                }
                if (model.getBorrowType() > 0) {
                    param.addParam("borrow.type", Operators.EQ, model.getBorrowType());
                }
                Date d = DateUtil.getDayEndTime(System.currentTimeMillis() / 1000);
                Date start = null;
                if (model.getTime() == 7) {
                    start = DateUtil.getDayStartTime(DateUtil.rollDay(d, -7).getTime() / 1000);
                    param.addParam("addTime", Operators.GTE, start);
                    param.addParam("addTime", Operators.LTE, d);
                } else if ((model.getTime() > 0) && (model.getTime() < 4)) {
                    start = DateUtil.getDayStartTime(DateUtil.rollMon(d, -model.getTime()).getTime() / 1000);
                    param.addParam("addTime", Operators.GTE, start);
                    param.addParam("addTime", Operators.LTE, d);
                }
                if (model.getStatus() > -1) {
                    param.addParam("status", model.getStatus());
                }
                if (StringUtil.isNotBlank(model.getStartTime())) {
                    start = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
                    param.addParam("addTime", Operators.GTE, start);
                }
                if (StringUtil.isNotBlank(model.getEndTime())) {
                    Date end = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
                    param.addParam("addTime", Operators.LTE, end);
                }
            }
            param.addPage(model.getPage(), model.getSize());
        }
        param.addOrder(OrderType.DESC, "id");

        PageDataList<BorrowTender> pageDataList = borrowTenderDao.findPageList(param);
        PageDataList<BorrowTenderModel> pageDateList = new PageDataList<BorrowTenderModel>();
        List<BorrowTenderModel> list = new ArrayList<BorrowTenderModel>();
        pageDateList.setPage(pageDataList.getPage());
        if (pageDataList.getList().size() > 0) {
            for (int i = 0; i < pageDataList.getList().size(); i++) {
                BorrowTender t = pageDataList.getList().get(i);
                BorrowTenderModel tm = BorrowTenderModel.instance(t);
                Borrow borrow = t.getBorrow();
                tm.setBorrowName(borrow.getName());
                tm.setAccountYes(borrow.getAccountYes());
                tm.setUserName(t.getUser().getUserName());
                tm.setPeriod(borrow.getPeriod());
                tm.setRealName(t.getUser().getRealName());
                tm.setBorrowUserName(borrow.getUser().getUserName());
                tm.setScales(borrow.getScales());
                tm.setBorrowId(borrow.getId());
                tm.setBorrowUuid(borrow.getUuid());
                tm.setStatus(t.getStatus());
                tm.setTimeLimit(borrow.getTimeLimit());
                tm.setBorrowTimeType(borrow.getBorrowTimeType());
                tm.setUser(null);
                tm.setBorrow(null);
                tm.setInterest(t.getInterest());
                tm.setApr(borrow.getApr());
                // 获取本次投资红包使用金额
                double redAccount = borrowTenderDao.getTenderUseRed(t.getId(), "borrow_tender");
                tm.setUseRadMoney(redAccount);
                // 获取加息券使用额度
                double rateApr = borrowTenderDao.getTenderUseRate(t.getId());
                tm.setUpApr(rateApr);
                // 使用体验金额度
                double experMoney = borrowTenderDao.getTenderUseExper(t.getId());
                tm.setUseExperMoney(experMoney);
                list.add(tm);
            }
        }
        pageDateList.setList(list);
        return pageDateList;
    }

    @Override
    public double hasTenderTotalPerBorrowByUserid(long borrowId, long userId) {
        return borrowTenderDao.hasTenderTotalPerBorrowByUserid(borrowId, userId);
    }

    @Override
    public PageDataList<BorrowTenderModel> list(long borrowId, int page, int size) {
        return borrowTenderDao.list(borrowId, page, size);
    }

    @Override
    public BorrowTender addTender(Borrow borrow, BorrowModel m) {
        borrow = borrowDao.find(borrow.getId());
        // 如果回调回来后 发现 实际借款金额+投资金额 超过了 借款金额 ，那么执行退款接口方法
        double accountYes = BigDecimalUtil.add(borrow.getAccountYes(), m.getMoney());
        if (accountYes > borrow.getAccount()) {

        }

        if (borrow.getStatus() != 1) {
            throw new BusinessException(MessageUtil.getMessage("MG03115F"), BusinessException.TYPE_JSON);
        }
        User user = m.getUser();

        // 检测用户投标使用的其它优惠信息
        checkActivity(m);

        // 现金投标金额
        /* double money = m.getMoney(); */

        // 投标使用红包总额
        double totalRedEnvelopeMoney = m.getTotalRedEnvelopeMoney();

        // 投标使用体验金总额
        double totalExperienceMoney = m.getTotalExperienceMoney();

		/* borrow.setScales(-1); */
        borrow.setIsTender(1);// 用于标记是投标的情况

        BorrowWorker worker = BorrowHelper.getWorker(borrow);
        /*
         * BorrowModel model = BorrowModel.instance(worker.prototype());
		 * model.setTenderBilNo(m.getTenderBilNo());
		 * model.setTenderBilDate(m.getTenderBilDate());
		 */

        // 投标校验
        // Account act = accountDao.getAccountByUserId(user.getUserId());
        // 红包、体验券与用户可用余额总和
        /* double totalUseMoney = BigDecimalUtil.add(act.getUseMoney(), totalRedEnvelopeMoney, totalExperienceMoney); */
        /*
         * if (money > totalUseMoney) {
		 * model.setMoney(totalUseMoney);
		 * } else {
		 * model.setMoney(money);
		 * }
		 */
        // 填充投标
        // BorrowTender tender = fillBorrowTender(model, user);
        // BorrowTender validTender = getValidTender(tender , m);
        BorrowTender validTender = borrowTenderDao.getTenderByBillNo(m.getTenderBilNo());

        double validAccount = validTender.getAccount();

        InterestCalculator ic = worker.interestCalculator(validAccount);
        validTender = worker.tenderSuccess(validTender, ic);
        /*
         * List<BorrowCollection> collectList = worker.createCollectionList(validTender, ic);
		 * borrowCollectionDao.save(collectList);
		 */

        // 实际冻结金额 = 实际投标金额 - 投标使用红包金额 - 投标使用体验券金额
        // double freezAmount = validTender.getAccount() - totalRedEnvelopeMoney - totalExperienceMoney;
        // 20151210 需求 投标成功 红包兑现，这里不再处理
        double freezAmount = validTender.getAccount();
        logger.info("平台投标业务处理：订单号" + m.getTenderBilNo() + "====投标金额：" + freezAmount + "====体验金：" + totalExperienceMoney);

        // 冻结投资人本金
        Global.setTransfer("borrow", borrow);
        Global.setTransfer("tenderAmount", validTender.getAccount());
        Global.setTransfer("money", freezAmount);
        Global.setTransfer("tender", validTender);
        Global.setTransfer("user", validTender.getUser());
        Global.setTransfer("redEnvelopeMoney", totalRedEnvelopeMoney);
        Global.setTransfer("experienceMoney", totalExperienceMoney);
        AbstractExecuter borrowTenderExecuter = ExecuterHelper.getExecuter(BorrowTenderExcuter.class);
        // 投资人投资冻结资金
        borrowTenderExecuter.execute(freezAmount, user, borrow.getUser());

        // 更新投标数，新手标使用，不管是否投标成功
        userStatisticsService.doExcute(user);

        // 处理红包
        // 20151210 需求 投标成功 红包兑现，这里不再处理
        /*
         * long[] redEnvelopeIds = m.getRedEnvelopeIds();
		 * if ((redEnvelopeIds != null) && (totalRedEnvelopeMoney > 0)) {
		 * Global.setTransfer("redEnvelopeIds", redEnvelopeIds);
		 * Global.setTransfer("tender", validTender);
		 * Global.setTransfer("totalRedEnvelopeMoney", totalRedEnvelopeMoney);
		 * AbstractExecuter redEnvelopeTenderExecuter = ExecuterHelper.doExecuter("redEnvelopeBorrowTenderExecuter");
		 * redEnvelopeTenderExecuter.execute(0, validTender.getUser());
		 * }
		 */

        // 处理加息券
        long rateInterestId = m.getRateInterestId();
        if (rateInterestId > 0) {
            Global.setTransfer("rateInterestId", rateInterestId);
            Global.setTransfer("tender", validTender);
            AbstractExecuter useRateInterestExecuter = ExecuterHelper.getExecuter(UseRateInterestExecuter.class);
            useRateInterestExecuter.execute(validTender.getAccount(), validTender.getUser());
        }

        // 处理体验券
        Global.setTransfer("experiences", m.getExperiences());
        Global.setTransfer("tender", validTender);
        AbstractExecuter experienceTenderExecuter = ExecuterHelper.getExecuter(ExperienceTenderExecuter.class);
        experienceTenderExecuter.execute(validTender.getAccount(), validTender.getUser());

        return validTender;
    }

    @Override
    public BorrowTender getTenderByBillNo(String subOrdId) {
        return borrowTenderDao.getTenderByBillNo(subOrdId);
    }

    /**
     * 检测用户投标使用的活动参数
     *
     * @param m
     */
    private void checkActivity(BorrowModel m) {
        // 检测用户使用红包状态
        // 20151210 需求 投标成功 红包兑现，这里不再处理
        /*
         * if ((m.getRedEnvelopeIds() != null) && (m.getTotalRedEnvelopeMoney() > 0)) {
		 * BaseExecuter userRedEnvelopeExecuter = (BaseExecuter) ExecuterHelper.doExecuter("userRedEnvelopeExecuter");
		 * if (!userRedEnvelopeExecuter.checkRedEnvelope(m.getRedEnvelopeIds(), m.getUser())) {
		 * throw new BusinessException(MessageUtil.getMessage("MF100002005"), BusinessException.TYPE_JSON);
		 * }
		 * }
		 */

        // 检测用户使用体验券状态
        if ((m.getExperiences() != null) && (m.getTotalExperienceMoney() > 0)) {
            ExperienceExecuter userExperienceExecuter = ExecuterHelper.getExecuter(ExperienceExecuter.class);
            if (!userExperienceExecuter.checkExperience(m.getExperiences(), m.getUser())) {
                throw new BusinessException(MessageUtil.getMessage("MF102007005"), BusinessException.TYPE_JSON);
            }
        }

    }


    /** 最新借款 * */
    @Override
    public List<BorrowTenderModel> getNewTenderList() {
        return borrowTenderDao.getNewTenderList();
    }

    /** 首页投资排行榜 * */
    @Override
    public List<RankModel> getRankList(String rank) {
        return borrowTenderDao.getRankList(rank);
    }

    /** 首页今天投资动态 */
    @Override
    public List<TenderDynamicModel> getTodayAddTenderList() {
        return borrowTenderDao.getTodayAddTenderList();
    }

    @Override
    public List<BorrowTenderModel> getTenderList(long borrowId) {
        List<BorrowTender> itemList = borrowTenderDao.findByProperty("borrow.id", borrowId);
        List<BorrowTenderModel> modelList = new ArrayList<BorrowTenderModel>();
        for (BorrowTender item : itemList) {
            BorrowTenderModel model = BorrowTenderModel.instance(item);
            model.setUserName(item.getUser().getUserName());
            model.setUserId(item.getUser().getUserId());
            // 查询使用体验券总和
            double experMoney = borrowTenderDao.getTenderUseExper(item.getId());
            model.setExperienceTenderAccount(experMoney);
            modelList.add(model);
        }
        return modelList;
    }

    /**
     * 根据id查询
     *
     * @param tenderId
     * @return
     */
    @Override
    public BorrowTender getTenderById(long tenderId) {
        return borrowTenderDao.find(tenderId);
    }

    @Override
    public int getTenderCountByDate(String date) {
        return borrowTenderDao.getTenderCountByDate(date);
    }

    @Override
    public double tenderAccount() {
        return borrowTenderDao.tenderAccount();
    }

    @Override
    public int getInvestUserCount() {
        return borrowTenderDao.getInvestUserCount();
    }

    @Override
    public double tenderAllInterest() {
        return borrowTenderDao.tenderAllInterest();
    }

    @Override
    public PageDataList<BorrowTenderModel> getBorrowTenderlist(BorrowTenderModel model) {
        return borrowTenderDao.getBorrowTenderlist(model);
    }

    @Override
    public PageDataList<BorrowTenderModel> getTenderRecordlist(BorrowTenderModel model) {
        return borrowTenderDao.getTenderRecordlist(model);
    }

    @Override
    public double[] getDistributedByTypeAndDate(int type, String date) {
        double[] arr = new double[5];
        for (int i = 0; i < 5; i++) {
            arr[i] = borrowTenderDao.getCountByTypeAndDate(type, i, date);
        }
        return arr;
    }

    @Override
    public double[] getCumulativeTenderByDate(String date) {
        int times = 0; // 循环次数
        if (date.isEmpty()) { // 当date为空则取最近5年的数据
            times = 5;
        } else { // 取当前年12个月的数据
            times = 12;
        }
        double[] arr = new double[times];
        for (int i = 0; i < times; i++) {
            String dateTime;
            if (times == 5) {
                dateTime = ((Calendar.getInstance().get(Calendar.YEAR) - 4) + i) + "";
            } else {
                dateTime = date + (i > 8 ? "-" : "-0") + (i + 1);
            }
            arr[i] = borrowTenderDao.getCumulativeTenderByDate(dateTime);
        }
        return arr;
    }

    @Override
    public int[] getTenderInfoByDate(String date) {
        int age1 = 0; // 年龄大于等于10小于20岁个数
        int age2 = 0; // 年龄大于等于20小于30岁个数
        int age3 = 0; // 年龄大于等于30小于40岁个数
        int age4 = 0; // 年龄大于等于40小于50岁个数
        int age5 = 0; // 年龄大于50岁个数
        int manCount = 0; // 男性个数
        int womanCount = 0; // 女性个数
        int[] arry = new int[19];
        List<String> cardIds = borrowTenderDao.getTenderCardIdsByDate(date);
        for (String cardId : cardIds) {
            if (StringUtil.isNotBlank(cardId)) {
                String year = cardId.substring(6, 10);
                int currentYear = Calendar.getInstance().get(Calendar.YEAR);
                int age = currentYear - Integer.parseInt(year);
                if ((age >= 10) && (age < 20)) {
                    age1++;
                } else if ((age >= 20) && (age < 30)) {
                    age2++;
                } else if ((age >= 30) && (age < 40)) {
                    age3++;
                } else if ((age >= 40) && (age < 50)) {
                    age4++;
                } else if (age > 50) {
                    age5++;
                }
                int length = cardId.length();
                char sexNum;
                if (length == 15) {
                    sexNum = cardId.charAt(length - 1);
                } else {
                    sexNum = cardId.charAt(length - 2);
                }
                // 获取性别 1:男,0:女
                int sex = sexNum % 2;
                if (sex == 0) {
                    womanCount++;
                } else {
                    manCount++;
                }
            }
        }
        arry[0] = age1;
        arry[1] = age2;
        arry[2] = age3;
        arry[3] = age4;
        arry[4] = age5;
        arry[5] = womanCount;
        arry[6] = manCount;
        for (int i = 0; i < 8; i++) {
            arry[i + 7] = userInfoDao.getEducationsCountByDateAndEducation(date, i + 1);
        }
        for (int i = 0; i < 4; i++) {
            arry[i + 15] = userInfoDao.getMaritalStatusCountByDateAndMaritalStatus(date, i);
        }
        return arry;
    }

    @Override
    public Object[][] getTenderAreaDistributedByDate(String date) {
        List<String> cardIds = borrowTenderDao.getTenderCardIdsByDate(date);
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (String cardId : cardIds) {
            String province = IdcardUtils.getProvinceByIdCard(cardId);
            int count = 1;
            if (map.containsKey(province)) {
                count = map.get(province) + 1;
            }
            map.put(province, count);
        }
        Collection<Integer> coll = map.values();
        Set<String> set = map.keySet();
        Object[] provinces = set.toArray();
        Object[] counts = coll.toArray();
        Object[][] result = new Object[2][counts.length];
        result[0] = provinces;
        result[1] = counts;
        return result;
    }

    @Override
    public boolean canTenderForCategory(User user, int category) {
        int count = borrowTenderDao.countTenderByUser(user, category);
        if (count == 0) {
            return true;
        }
        return false;
    }

    @Override
    public BorrowTender find(String tenderBilNo) {
        return borrowTenderDao.getTenderByBillNo(tenderBilNo);
    }

    @Override
    public void update(BorrowTender borrowTender) {
        borrowTenderDao.update(borrowTender);
    }

    @Override
    public double todayTenderAccount() {
        return borrowTenderDao.todayTenderAccount();
    }

    @Override
    public BorrowTender tenderSuccess(BorrowTender tender, InterestCalculator calculator) {
        // 总共需要还款金额
        double repayment_account = calculator.repayTotal();
        // tender标利息总和
        double totalInterest = BigDecimalUtil.sub(repayment_account, tender.getAccount());
        tender.setRepaymentAccount(repayment_account);
        tender.setInterest(totalInterest);
        tender.setWaitAccount(repayment_account);
        tender.setWaitInterest(totalInterest);
        tender.setStatus(BorrowTenderStatus.SUCCESS);
        borrowTenderDao.modifyBorrowTender(tender);
        return tender;
    }

    @Override
    public List<BorrowTenderModel> repaymentExperience() {
        return borrowTenderDao.repaymentExperience();
    }

    @Override
    public void repaymentStatus(BorrowTenderModel tenderModel, User user) {
        long tender_id = tenderModel.getId();
        double money = tenderModel.getInterest(); // 体验金获取利息
        if (money > 0) {
            borrowTenderDao.modifyTenderStatus(tender_id, 3, money);
            long borrow_id = tenderModel.getBorrowId();
            long user_id = tenderModel.getUserId();
            borrowCollectionDao.modifyCollectionStatus(1, borrow_id, tender_id, user_id, money);

            // 插入待收记录
            TppTradeLog tppTradeLog = new TppTradeLog();
            if ("baofoo".equals(SystemConfigHelper.getSystemConfig(Nid.TPP_NAME).getValue())) {// 宝付
            	tppTradeLog = collectService.createOrder(money, user_id);
                ReturnResult returnResult = collectService.requestOrder(tppTradeLog);
                if (!returnResult.isSuccess()) {
                    throw new RedEnvelopeException("转账接口请求失败!", RedEnvelopeException.TYPE_JSON);
                }
            } else {// 新浪
            	tppTradeLog = new TppTradeLog(SystemConfigHelper.getValue(Nid.MEMBER_EMAIL), user.getTppUserCustId(),
                        SinaConstant.IDENTITY_TYPE_EMAIL, money, OrderNoUtils.getSerialNumber(),
                        SinaConstant.TRADE_STATUS_WAIT_PAY, SinaConstant.TRADE_CODE_1001,
                        SinaConstant.TRADE_SERVICE_RED_ENVELOPE_EXCHAGE, "");
            	tppTradeService.collTrade(tppTradeLog);
            }
            // 生成日志
            Global.setTransfer("money", money);
            Global.setTransfer("user", user);
        	AbstractExecuter moneyExecuter = ExecuterHelper.getExecuter(InterestExchangeExecuter.class);
            moneyExecuter.execute(money, user, new User(SystemConfigs.ADMIN_ID));
        }
    }

    @Override
    public PageDataList<BorrowTenderModel> listInvestor(long borrowId, int page, int size) {
        return borrowTenderDao.listInvestor(borrowId, page, size);
    }

    @Override
    public double getTenderAmountByDate(String date) {
        return borrowTenderDao.getTenderAmountByDate(date);
    }

    @Override
    public BorrowTender getBorrowTenderAmountByBorrowId(long borrowId, long userId) {
        return borrowTenderDao.getBorrowTenderAmountByBorrowId(borrowId,userId);
    }
}
